#include "InstructorWindow.h"
#include "ui_InstructorWindow.h"
#include <QDebug>


InstructorWindow::InstructorWindow(QWidget *parent, AdminData* data) : QMainWindow(parent), ui(new Ui::InstructorWindow)
{
    this->data = data;
    ui->setupUi(this);

    ui->tabWidget->setCurrentWidget(ui->editOutlineTab);
    ui->editOutlineStackedWidget->setCurrentWidget(ui->selectOutlinePage);
    ui->selectOutlineTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->selectOutlineTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->selectOutlineTable->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->selectOutlineTable->setColumnCount(4);
    QStringList headerList;
    headerList << "Course Code" << "Course Name" << "Duration" << "Status";
    ui->selectOutlineTable->setHorizontalHeaderLabels(headerList);
    courseList = data->getCourses();

    for (int i = 0; i < data->getCourses().count(); ++i)
    {
        QString code = data->getCourses().at(i).getCode();
        QString name = data->getCourses().at(i).getName();
        QString title = "The University of Western Ontario \n London, Canada \n " + code + " -- " + name + "\n Course Outline \n";
        CourseOutline* outline = new CourseOutline(title, *(new QVector<OutlineSection> (data->getOutlineSections())), "new");
        courseList[i].setOutline(*outline);
        qDebug() << courseList[i].getOutline().getTitle();
//        QString duration =
        QString status = courseList[i].getOutline().getStatus();
        QTableWidgetItem *codeItem = new QTableWidgetItem; codeItem->setText(code);
        QTableWidgetItem *nameItem = new QTableWidgetItem; nameItem->setText(name);
        QTableWidgetItem *statusItem = new QTableWidgetItem; statusItem->setText(status);
        int row = ui->selectOutlineTable->rowCount();
        ui->selectOutlineTable->insertRow(row);
        ui->selectOutlineTable->setItem(row, 0, codeItem);
        ui->selectOutlineTable->setItem(row, 1, nameItem);
        ui->selectOutlineTable->setItem(row, 3, statusItem);
    }
    for (int i = data->getOutlineSections().count() - 1; i >= 0; --i)
    {
        QString sectionTitle = data->getOutlineSections().at(i).getTitle();
        QListWidgetItem* sectionItem = new QListWidgetItem(sectionTitle,0,0);
        ui->sectionListWidget->addItem(sectionItem);
    }

    courses = data->getCourses();
    for(int i = 0; i < courses.size(); i++){
        if(courses.at(i).getOutline().getStatus()=="approved"){
            ui->outlineList->addItem(courses.at(i).getName());
            approvedCourses.append(courses.at(i));
        }
    }
 }

InstructorWindow::~InstructorWindow()
{
    delete ui;
}

void InstructorWindow::on_editButton_clicked()
{
    selectedRow = ui->selectOutlineTable->currentRow();

    if (selectedRow == -1)
        return;

    ui->editOutlineStackedWidget->setCurrentWidget(ui->editOutlinePage);
    ui->sectionListWidget->setCurrentRow(0);

    selectedCourse = &(data->getCourses()[selectedRow]);
    selectedCourse = &(courseList[selectedRow]);

    for (int i = 0; i < ui->sectionListWidget->count(); ++i)
    {
        int j = (data->getOutlineSections().count() - 1)  - i;
        ui->editSectionStackedWidget->insertWidget(i, new outlineSectionPage(this, data));
        ui->editSectionStackedWidget->setCurrentIndex(i);
        ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->descriptionTextEdit->setText(data->getOutlineSections().at(j).getDescription());
        ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->descriptionTextEdit->setEnabled(false);
        if(data->getOutlineSections().at(j).isDefTextRequired())
        {
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->requiredTextEdit->setText(data->getOutlineSections().at(j).getText());
        }
        else
        {
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->requiredLabel->setVisible(false);
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->requiredTextEdit->setVisible(false);
            if (courseList[selectedRow].getOutline().getStatus() == "new")
            {
                ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->instructorTextEdit->setText(data->getOutlineSections().at(j).getText());
            }
        }
        ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->requiredTextEdit->setEnabled(false);

        if (courseList[selectedRow].getOutline().getStatus() != "new")
        {
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->instructorTextEdit->setText(courseList[selectedRow].getOutline().getOutlineSectionList()[j].getText());
        }

        if (!data->getOutlineSections().at(j).doesHaveDates())
        {
            ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->openScheduledDialogButton->setEnabled(false);
        }
        ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->commentTextEdit->setText(data->getOutlineSections().at(j).getComments());
        ((outlineSectionPage*)ui->editSectionStackedWidget->currentWidget())->getUi()->commentTextEdit->setEnabled(false);
    }
    ui->editSectionStackedWidget->setCurrentIndex(0);
}

void InstructorWindow::on_sectionListWidget_currentRowChanged(int currentRow)
{
    ui->sectionTitle->setText(ui->sectionListWidget->currentItem()->text());
    ui->editSectionStackedWidget->setCurrentIndex(currentRow);
}

void InstructorWindow::on_cancelButton_clicked()
{
    for(int i = 0; i < ui->sectionListWidget->count(); ++i)
    {
        QWidget* sectionPage = ui->editSectionStackedWidget->widget(i);
        ui->editSectionStackedWidget->removeWidget(sectionPage);
        delete sectionPage;
    }
    selectedRow = -1;
    selectedCourse = NULL;
    ui->editOutlineStackedWidget->setCurrentWidget(ui->selectOutlinePage);
}

void InstructorWindow::on_saveOutlineButton_clicked()
{
    for (int i = 0; i < ui->sectionListWidget->count(); ++i)
    {
        int j = (data->getOutlineSections().count() - 1)  - i;
        QWidget* sectionPage = ui->editSectionStackedWidget->widget(i);

        qDebug() << ui->sectionListWidget->item(i)->text();
        qDebug() << courseList[selectedRow].getOutline().getOutlineSectionList()[j].getTitle();
        qDebug() << ((outlineSectionPage*)sectionPage)->getUi()->instructorTextEdit->toPlainText();
        courseList.at(selectedRow).getOutline().getOutlineSectionList()[j].setText(
                    ((outlineSectionPage*)sectionPage)->getUi()->instructorTextEdit->toPlainText());
        qDebug() << courseList.at(selectedRow).getOutline().getOutlineSectionList()[j].getText();
    }

    for(int i = 0; i < ui->sectionListWidget->count(); ++i)
    {
        QWidget* sectionPage = ui->editSectionStackedWidget->widget(i);
        ui->editSectionStackedWidget->removeWidget(sectionPage);
        delete sectionPage;
    }
    selectedRow = -1;
    selectedCourse = NULL;
    ui->editOutlineStackedWidget->setCurrentWidget(ui->selectOutlinePage);

    courseList[selectedRow].getOutline().setStatus("incomplete");
    data->setCourse(&courseList);
    qDebug() << data->getCourses()[selectedRow].getOutline().getStatus();

    AdminErrorDialog *aedPer=new AdminErrorDialog(this,"Data has been saved.");
    aedPer->setModal(true);
    aedPer->exec();
}

void InstructorWindow::on_submitOutlineButton_clicked()
{
    bool submit = true;
    for (int i = 0; i < ui->sectionListWidget->count(); ++i)
    {
        int j = (data->getOutlineSections().count() - 1)  - i;
        QWidget* sectionPage = ui->editSectionStackedWidget->widget(i);

        if (!data->getOutlineSections()[j].isOptional() && ( ((outlineSectionPage*)sectionPage)->getUi()->instructorTextEdit->toPlainText() == "") )
        {
            submit = false;
            AdminErrorDialog *aedPer=new AdminErrorDialog(this, data->getOutlineSections()[j].getTitle() + "is Mandeto");
            aedPer->setModal(true);
            aedPer->exec();
            break;
        }
    }
    if (submit)
    {
        for (int i = 0; i < ui->sectionListWidget->count(); ++i)
        {
            int j = (data->getOutlineSections().count() - 1)  - i;
            QWidget* sectionPage = ui->editSectionStackedWidget->widget(i);

            courseList.at(selectedRow).getOutline().getOutlineSectionList()[j].setText(((outlineSectionPage*)sectionPage)->getUi()->instructorTextEdit->toPlainText());
        }

        for(int i = 0; i < ui->sectionListWidget->count(); ++i)
        {
            QWidget* sectionPage = ui->editSectionStackedWidget->widget(i);
            ui->editSectionStackedWidget->removeWidget(sectionPage);
            delete sectionPage;
        }

        selectedRow = -1;
        selectedCourse = NULL;
        ui->editOutlineStackedWidget->setCurrentWidget(ui->selectOutlinePage);

        courseList[selectedRow].getOutline().setStatus("under review");
        data->setCourse(&courseList);
        qDebug() << data->getCourses()[selectedRow].getOutline().getStatus();

        AdminErrorDialog *aedPer=new AdminErrorDialog(this,"outline has been submitted to chair for review");
        aedPer->setModal(true);
        aedPer->exec();
    }
}

void InstructorWindow::on_outlineList_itemDoubleClicked(QListWidgetItem *item)
{
    ui->stackedWidget->setCurrentIndex(1);
    courseOutline = approvedCourses.at(ui->outlineList->currentRow()).getOutline();
    ui->outline->append(approvedCourses.at(ui->outlineList->currentRow()).getName() + "\n");
    for(int i = 0; i < courseOutline.getOutlineSectionList().size(); i++){
        if(!courseOutline.getOutlineSectionList().at(i).getTitle().isNull()){
            ui->outline->append(courseOutline.getOutlineSectionList().at(i).getDescription() + "\n");
            ui->outline->append(courseOutline.getOutlineSectionList().at(i).getText() + "\n \n");
        }
    }
    ui->outline->setReadOnly(true);
}







